package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class SecondResourceFilterHandler implements ResourceFilterHandler{
    private ResourceFilterHandler resourceFilterHandler;

    public void setNextHandler(ResourceFilterHandler resourceFilterHandler) {
        this.resourceFilterHandler = resourceFilterHandler;
    }

    @Override
    public void filter(List<Resource> list, TreeItemVo root) {
        root.getChildren().stream().map(r -> {
            List<TreeItemVo> second = list.stream().filter(re -> {
                if (re.getParentResourceNo().equals(r.getId())) {
                    return true;
                } else {
                    return false;
                }
            }).map(re -> {
                TreeItemVo vo = new TreeItemVo();
                vo.setId(re.getResourceNo());
                vo.setLabel(re.getResourceName());
                return vo;
            }).collect(Collectors.toList());
            r.setChildren(second);
            return r;
        }).collect(Collectors.toList());
        if(null != resourceFilterHandler){
            resourceFilterHandler.filter(list,root);
        }
    }
}
